package org.jeecg.common.util;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.config.binlog.SpringContextUtil;

import javax.sql.DataSource;
import java.sql.*;

/**
 * @author gbstart
 * @create 2024-06-28 8:12
 */
@Slf4j
public class JdbcUtil {

    /**
     * 日志记录时间
     */
    public static Timestamp getTime( String table,String id) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection connection = null;
        try {
            //获取数据源
            DataSource dataSource = SpringContextUtil.getBean(DataSource.class);
            connection = dataSource.getConnection();
            // 执行sql获取表数据
            String preSql = "SELECT logtime FROM "+table+" WHERE id=?";
//            String preSql = "SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? and TABLE_NAME = ?";
            ps = connection.prepareStatement(preSql);
//            ps.setString(1, table);
            ps.setString(1, id);
            rs = ps.executeQuery();
//            Map<String, Colum> map = new HashMap<>(rs.getRow());
            Timestamp logtime = null;
            while (rs.next()) {
                logtime = rs.getTimestamp("logtime");
//
            }
            ps.close();
            rs.close();
            connection.close();
            return logtime;
        } catch (SQLException e) {
//            log.error("load db conf error, db_table={}:{} ", db, table, e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        return null;
    }



}
